//
//  calculatHeightForReplayCell.m
//  IfengNews
//
//  Created by tianhy on 13-1-7.
//
//

#import "CalculatHeightForReplayCell.h"
#import "Define.h"
#import "CLGloble.h"
#import "DetailUtil.h"

@implementation CalculatHeightForReplayCell

#pragma mark -
#pragma mark Calculat Height Methods


+ (float)calculationCommonCommentHeightWithData:(NSDictionary *)dataDic
{
    float height = 0.0;
    
    
    height = height + intervalFromTop;
    height = height + zoneHeightALL;
    
    CGSize commentSize = [[CLGloble sharedInstance] calculateSizeWithFont:commentFont
                                              withText:[dataDic sgrGetStringForKey:@"comment_contents"]
                                           withMaxSize:commentMaxSize
                                         withLineSpace:commentLineSpace
                                       withAllowIndent:NO];
        
  height = height + commentSize.height+intervalZoneAndComment + intervalCommentAndBottom;
    return height;
}


//计算整个cell，完整显示时的高度
+ (float)calculationHeightWithData:(NSDictionary *)dic isShowAllComments:(BOOL)isShow
{
  float topMargin = intervalFromTop;      //盖楼时，地区视图到顶部的距离
  float currentZoneHeight = zoneHeightALL;   //地区视图的高度
  float intervalBetweenZoneAndComment = intervalZoneAndComment;    //地区视图到楼层视图的高度
  float floorsHeight = [CalculatHeightForReplayCell calculationFloorsHeight:dic isShowAllComments:isShow];
  float intervalBetweenFloorsAndComment = 18;  //当前评论到楼层视图的高度
  float currentCommentHeight = [self calculationCurrentCommentHeight:dic];
  float intervalFromBottom = intervalCommentAndBottom;
  float totalHeight = topMargin + currentZoneHeight + intervalBetweenZoneAndComment + intervalBetweenFloorsAndComment + floorsHeight + currentCommentHeight +intervalFromBottom;
  return totalHeight;
}

+ (float)calculationCurrentCommentHeight:(NSDictionary *)data
{
  NSString *commentStr = [data sgrGetStringForKey:@"comment_contents"];
  
//  float commentheight = [[Globle shareInstance] getSizeOfText:commentStr
//                                                     withFont:commentFont
//                                                  withMaxSize:commentMaxSize].height;
//    CGSize commentSize = [DetailUtil lineByWidth:commentStr
//                                           Width:commentMaxSize.width
//                                            Font:commentFont
//                                      lineHeight:26];
    CGSize commentSize = [[CLGloble sharedInstance] calculateSizeWithFont:commentFont
                                              withText:commentStr
                                           withMaxSize:commentMaxSize
                                         withLineSpace:commentLineSpace
                                       withAllowIndent:NO];
//  return commentSize;
    return commentSize.height;
}

//计算多个楼层的总的高度（只计算楼层，不含当前评论的高度）
+ (float)calculationFloorsHeight:(NSDictionary *)data isShowAllComments:(BOOL)isShow
{
    @autoreleasepool {
        NSArray *parentCommentArray = [data sgrGetArrayForKey:@"parent"];
        if(!parentCommentArray || ![parentCommentArray isKindOfClass:[NSArray class]]
           ||[parentCommentArray count] <= 0){
            return 0;
        }
        
        float totalHeight = 0;
        int count = [parentCommentArray count];
        if([parentCommentArray count] > 6 && !isShow)   //需要显示隐藏评论的按钮
        {
            //表示折叠起来的评论
            
            NSDictionary *firstFloorComment = [parentCommentArray sgrGetDictionaryForIndex:count-1];
            float firstFloorHeight = [self calculationOneFloorHeight:[firstFloorComment sgrGetStringForKey:@"comment_contents"]];
            float showHiddenViewHeight = 50;
            totalHeight = firstFloorHeight + showHiddenViewHeight;
            
            for (int i = 2; i >= 0; i--) {
                NSString *comment = [[parentCommentArray objectAtIndex:i] sgrGetStringForKey:@"comment_contents"];
                totalHeight += [self calculationOneFloorHeight:comment];
            }
            return totalHeight;
        }
        else      //直接显示所有楼层的评论
        {
            for(NSDictionary *dic in parentCommentArray)
            {
                NSString *commentStr = [dic sgrGetStringForKey:@"comment_contents"];
                float singleFloorHeight = [self calculationOneFloorHeight:commentStr];
                totalHeight = totalHeight + singleFloorHeight;
            }
            return totalHeight;
        }
    }
}


//计算盖楼时，单独一个楼层的高度
+ (float)calculationOneFloorHeight:(NSString *)commentStr
{
  float topMargin = 12.0;                                 //地域label距离上方的距离
  float zoneLabelHeight = zoneHeightALL;                     //地域label的高度
  float interval = 17.0;                                  //地域label和评论内容的间隔

    float commentHeight = [[CLGloble sharedInstance] calculateSizeWithFont:commentFont
                                               withText:commentStr
                                            withMaxSize:commentMaxSizeInFloors
                                          withLineSpace:commentLineSpace
                                        withAllowIndent:NO].height;
    
  float interalFromBottom = 12;                           //评论内容到底部的间隔
  return topMargin + zoneLabelHeight + interval + commentHeight + interalFromBottom;
}


@end
